package com.yubest.demo.service;

import com.yubest.demo.enums.OrderTypeEnum;
import com.yubest.demo.util.SpringUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author hweiyu
 * @Description
 * @Date 2021/3/19 11:28
 */
public class OrderStrategy {

    private static Map<OrderTypeEnum, OrderService> SERVICE_MAP = new HashMap<>(16);

    //初始化，将service缓存到内存中
    static {
        Map<String, OrderService> map = SpringUtil.getBeansOfType(OrderService.class);
        for (Map.Entry<String, OrderService> entry : map.entrySet()) {
            SERVICE_MAP.put(entry.getValue().getType(), entry.getValue());
        }
    }

    /**
     * 根据订单类型选择对应的处理service
     * @param orderType
     * @return
     */
    public static OrderService choose(String orderType) {
        OrderTypeEnum type = OrderTypeEnum.of(orderType);
        return SERVICE_MAP.get(type);
    }
}
